var util = require('../../../../utils/util.js');
var wxcache = require('../../../../utils/wxcache.js');
var api = require('../../../../config/api.js');
var user = require('../../../../services/user.js');
var app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNumber :'',
    newpwdInput:'',
    newpwdInput2:'',
    oldpwdInput:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var phoneNumber = wxcache.get("phoneNumber")
    this.setData({
      phoneNumber: phoneNumber
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  newpwdInput: function(e){
    this.setData({
      newpwdInput:e.detail.value
    })
  },
  newpwdInput2: function (e) {
    this.setData({
      newpwdInput2: e.detail.value
    })
  },
  oldpwdInput: function (e) {
    this.setData({
      oldpwdInput: e.detail.value
    })
  },

  /***
   * 检验密码是否和原密码一样
   * 检验重复密码是否一样
   * 检验新密码输入的规则
   * 入库
   */
  save:function(e){
    var that = this;

    var newpwdInput = that.data.newpwdInput;
    var newpwdInput2 = that.data.newpwdInput2;
    var oldpwdInput = that.data.oldpwdInput;
    
    util.request(api.LoginURL, { mobile: that.data.phoneNumber, password: oldpwdInput},'POST')
        .then(res=>{
          console.log(res)
          if(res.code == 200){
             
            if (newpwdInput != newpwdInput2) {
              wx.showModal({
                title: '提示',
                content: '两次输入密码不一样',
                showCancel: false
              })
            } else {
              // var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/
              // 密码大于3位就可以
              if (newpwdInput.length > 3) {

                var data = {}
                data.mobile = that.data.phoneNumber
                data.password = newpwdInput
                util.request(api.PasswordUpdate, data, 'POST').then(res => {

                  wx.switchTab({
                    url: '/pages/ucenter/index/index',
                  })

                  wx.showToast({
                    title: '密码修改成功',
                    icon: 'success',
                    duration: 2000,
                    mask: true
                  })

                }, function (error) {

                  wx.showModal({
                    title: '提示',
                    content: '密码修改失败，请稍后重试',
                  })

                })
              } else {
                wx.showModal({
                  title: '提示',
                  content: '密码太短',
                  showCancel: false
                })
              }
            }   
          }else{
            wx.showModal({
              title: '提示',
              content: '原密码输入错误',
              showCancel: false
            })
            return;
          }

      }) 
  }

  
})